package com.chen.bpsptsystem.common;


import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class TokenCache {

    private static Logger logger= LoggerFactory.getLogger(TokenCache.class);

    public static final String TOKEN_PREFIX = "token_";

    private static LoadingCache<String,String> localcache = CacheBuilder.newBuilder().initialCapacity(1000)
            .maximumSize(10000).expireAfterAccess(12, TimeUnit.HOURS).build(new CacheLoader<String, String>() {
                @Override
                public String load(String s) throws Exception {
                    return "null";
                }
            });
    public static void setKey(String key,String value){
        localcache.put(key,value);
    }

    public static String getKey(String key){
        String value = null;
        try {
            value = localcache.get(key);
            if ("null".equals(value)){
                return null;
            }
            return value;
        }catch (Exception e){
            logger.error("localcache get error",e);
        }
        return null;
    }

    public static void main(String[] args) throws Exception{
        TokenCache.setKey( TokenCache.TOKEN_PREFIX ,UUID.randomUUID().toString() );
        for (int i = 0; i < 15; i++) {
            System.out.println(( UUID.randomUUID().toString() ));
            Thread.sleep( 1000 );
            System.out.println(TokenCache.getKey( TokenCache.TOKEN_PREFIX ));
        }

    }
}
